var passport = require('passport');

module.exports = {
  login: function (req, res, next) {
    var auth = passport.authenticate('local', function (err, user) {
      if (err) return next(err);
      if (!user) {
        return res.send(user);
      }
      req.logIn(user, function (err) {
        if (err) return next(err);
        return res.send(user);
      })
    });
    auth(req, res, next);
  },
  logout: function (req, res, next) {
    req.session.destroy();
    req.logout();
    res.send('logout')
  },
  isLogin: function (req, res) {
    if (!req.isAuthenticated()) {
      return res.send({bool: false})
    } else {
      return res.send({bool: true, user: req.user})
    }
  },
  isAuthenticated: function () {
    return function (req, res, next) {
      if (req.isAuthenticated()) {
        next()
      } else {
        return res.send({bool: false});
      }
    }
  }
};